feat: add useNativeTokenCount flag to skip token counting API calls#2255
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Issue: This PR adds a new configuration parameter to every public model provider constructor (Anthropic, Bedrock, Gemini, LlamaCpp, OpenAIResponses). This is a public API change that affects all customers. Per the API Bar Raising guidelines, the PR should:
Suggestion: Please update the PR description with:
|
|
Issue: The PR checklist items are all unchecked — Suggestion: Please complete the PR template:
|
Review SummaryAssessment: Request Changes This PR adds a Key Areas to Address
The implementation is functional and well-tested; the main concerns are around API design process and code organization. |
Description
Model providers with native token counting APIs (Bedrock, Anthropic, Gemini, OpenAI Responses, llama.cpp) make an additional API call on every
count_tokens()invocation. In scenarios where latency or cost of these calls is undesirable — such as high-frequency proactive compression checks — users need a way to opt out and fall back to the local estimator.Each provider that overrides
count_tokens()with a native API call gains a new optionaluse_native_token_countfield in its config:Defaults to True (current behavior). Available on BedrockConfig, AnthropicConfig, GeminiConfig, OpenAIResponsesConfig, and LlamaCppConfig — the five providers that override count_tokens() with native API calls. The base Model class is unaffected since it only has the local estimator implementation.
Related Issues
Python port of strands-agents/sdk-typescript#1009
Documentation PR
NA
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.